//
//  RCUserPhoneRegFullfilNickVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-26.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserPhoneRegFullfilNickVC.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "RCUserPhoneRegVC.h"
#import "IManager+RegisterRequest.h"
#import "RCPhoneVerifyDataModel.h"
#import "AppDelegate.h"
#import "RCLoginDataModel.h"
#import "GloableEngineService.h"

@interface RCUserPhoneRegFullfilNickVC ()<IManagerDelegate,UITextFieldDelegate>{
    NSString    *_regPhoneNum;
    BOOL        isCheck;
    
    UITextField *_userNameLabel;
    UITextField *_userPassword;
    UIButton    *_regBtn;
    
    int64_t      _reqID;
}

@end

@implementation RCUserPhoneRegFullfilNickVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id) initWithRegPhone:(NSString *)phoneNumber
{
    self = [super init];
    if (self) {
        _regPhoneNum = [phoneNumber copy];
    }
    return self;
}

- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [_regPhoneNum release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    for (int i= 0; i < 2; i ++) {
        UIImageView *bg1 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 12+60*i, 320-14*2, 47)] autorelease];
        UIImage *img1 = [UIImage imageNamed:@"form_bg1"];
        bg1.image = img1;
        [self.view addSubview:bg1];
        
        UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15+9, 15, 70, 16)];
        [bg1 addSubview:phoneLabel];
        [phoneLabel setFont:[UIFont fontWithName:kFontNameLightHeiti size:16]];
        phoneLabel.textColor = [UIColor colorWithHex:0xff979797];
        
        
        
        UITextField *filed = [[UITextField alloc] initWithFrame:CGRectMake(100, 25+60*i, 200, 20)];
        [self.view addSubview:filed];
        [filed setBackgroundColor:[UIColor clearColor]];
        filed.clearButtonMode = UITextFieldViewModeWhileEditing;
        filed.font = [UIFont systemFontOfSize:16];
        //    _userNameLabel.tag = kTagUserName;
        filed.returnKeyType = UIReturnKeyDone;
        filed.delegate = self;
        
        if (i == 0) {
            phoneLabel.text = @"昵称";
            _userNameLabel = filed;
        }else{
            phoneLabel.text = @"密码";
            _userPassword = filed;
            _userPassword.secureTextEntry = YES;
        }
        [phoneLabel release];
        [filed release];
    }
    
    UIButton *checkBox = [[UIButton alloc] initWithFrame:CGRectMake(14, 125, 20, 20)];
    [self.view addSubview:checkBox];
    [checkBox addTarget:self action:@selector(checkLawClick:) forControlEvents:UIControlEventTouchUpInside];
    [checkBox setBackgroundImage:[UIImage imageNamed:@"icon_law_uncheck"] forState:UIControlStateNormal];
//    [checkBox setBackgroundColor:[UIColor yellowColor]];
    [checkBox release];
    
    UILabel *lawLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 130, 250, 15)];
    [lawLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:lawLabel];
    lawLabel.font = [UIFont systemFontOfSize:10];
    [lawLabel release];
    lawLabel.text = @"我已经看过并同意如意行《用户使用协议》";
    
    _regBtn = [[UIButton alloc] initWithFrame:CGRectMake(14, 150, 320-28, 40)];
    [_regBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.view addSubview:_regBtn];
    UIImage *loginIMG = [UIImage imageNamed:@"login_btn"];
    [_regBtn setBackgroundImage:loginIMG forState:UIControlStateNormal];
    [_regBtn addTarget:self action:@selector(regPress:) forControlEvents:UIControlEventTouchUpInside];
    [_regBtn release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) regPress:(id)sender
{
    
    if (!isCheck) {
        [RCAppDelegate alertWithCustomizeString:@"请勾选法律协议"];
        return;
    }
    
    [self.view endEditing:YES];

    //检查用户名
    NSString *userName = _userNameLabel.text;
    if (userName == nil) {
        [self.view endEditing:YES];
        [RCAppDelegate alertWithCustomizeString:@"请填写用户名"];
        return;
    }
//    NSError * error = NULL;
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})$" options:0 error:&error];
//    NSTextCheckingResult * result = [regex firstMatchInString:userName options:0 range:NSMakeRange(0, [userName length])];
//    if (!result || (regex.numberOfCaptureGroups + 1) != result.numberOfRanges) {
//        [RCAppDelegate alertWithCustomizeString:@"请正确填写用户名"];
//
//        return;
//    }
    
    //检查密码
    NSString *password = [_userPassword.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([password length] == 0) {
        [RCAppDelegate alertWithCustomizeString:@"请输入密码"];
        return;
    }
    
    _reqID = [[IManager sharedInstance] requestEmail:_regPhoneNum withUserName:_userNameLabel.text withPassword:_userPassword.text withPhoneType:YES withDelegate:self];

}

-(void)  checkLawClick:(id)sender
{
    isCheck = !isCheck;
    UIButton *t = (UIButton *)sender;
    if (isCheck) {
        [t setBackgroundImage:[UIImage imageNamed:@"icon_law_unchecked"] forState:UIControlStateNormal];
    }else{
        [t setBackgroundImage:[UIImage imageNamed:@"icon_law_uncheck"] forState:UIControlStateNormal];
    }
//    [t setBackgroundColor:[UIColor blueColor]];
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"提交失败"];
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
//    RCEmailRegDataModel
    if ([dataModel isKindOfClass:[RCEmailRegDataModel class]]) {
        //重新获取验证码
        RCEmailRegDataModel *model = (RCEmailRegDataModel *)dataModel;
        if (model.errorNO == RCErrorTypeNoError) {
            //注册成功
            RCLoginDataModel *loginInfo = [[RCLoginDataModel alloc] init];
            loginInfo.nickName = _userNameLabel.text;
            loginInfo.bduss    = model.bduss;
            [[GloableEngineService sharedInstance] saveUserLoginInfo:loginInfo];
            [loginInfo release];
            [[[self.navigationController parentViewController] navigationController] popToRootViewControllerAnimated:YES];
        }else{
            [RCAppDelegate alertWithCustomizeString:model.errorMsg];
        }
    }
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self regPress:nil];
    return YES;
}
@end
